<?php

require_once 'socket.php';

class NetClient extends Socket
{

    function post($msg)
    {
        $params = func_get_args();
        return call_user_method_array('write', $this, $params);
    }

    function get()
    {
        $params = func_get_args();
        return call_user_method_array('read', $this, $params);
    }

    function request($msg)
    {
        $start   = microtime(true);

        if (func_num_args() > 1) {
            $timeout = func_get_arg(1);
        } else {
            $timeout = $this->_M_param->timeout_write();
        }

        $this->post($msg, $timeout);

        if ($timeout->out < (microtime(true) - $start)) {
            throw new Exception();
        }

        return $this->get(TimeOut($timeout->out - (microtime(true) - $start)));
    }

    function response()
    {
        $response = '';
        end($this->_M_history);
        while (is_a($msg = current($this->_M_history), 'ResponseMessage')) {
            prev($this->_M_history);
            $response .= $msg->value();
        }
        return $response;
    }

    function has_response()
    {
        $last_msg =& end($this->_M_history);
        return is_object($last_msg) && is_a($last_msg, 'ResponseMessage') && strlen($last_msg->value());
    }
}

?>